// pages/confirm/confirm.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    display:"none",
    _num: 1,
    order_no:''
  },
  liuyan:function(e){
      this.setData({
        liuyan:e.detail.value
      })
  },
  show_payment:function(){
 
    this.setData({
      display:"block"
    })
    
  },
  hideview: function () {
    let  that=this;
    var type=that.data.type;
    var attrid = that.data.attrid;
    if(type==1){
      wx.showModal({
        title: '提示',
        content: '您确定参加团购吗',
        success:function(e){
          if(e.confirm==1){
            wx.showLoading({
              title: '请稍后',
            })
            wx.request({
              url: app.data.Url +'goods/SpellInsert',
              data:{
                uid:wx.getStorageSync('userInfo').uid,
                attrid:attrid,
                num:that.data.num,
                remark: that.data.liuyan ? that.data.liuyan:''
              },
              method:'post',
              success:function(e){
                  if(e.data.code==1){
                    wx.showToast({
                      title: '团购成功',
                      icon: 'none',
                      duration:1000,
                      success:function(){
                        setTimeout(function(){
                            wx.navigateTo({
                              url: '/pages/collage/collage',
                            })
                        }, 1000)
                      }
                    })
                  }else{
                    wx.hideLoading();
                    wx.showToast({
                      title: e.data.msg,
                      icon:'none'
                    })
                  }
              }
            })
            that.setData({
              display: "none"
            })
          }
        }
      })
    }
    
  },
  pay:function(){
    wx.request({
      url: app.data.Url + 'order/orderPayParams',
      data: {
        order_no: that.data.order_no,
        title: '智慧社区订单支付',
        uid: wx.getStorageSync('userInfo').uid
      },
      method: 'post',
      success: function (res) {
        console.log(res.data.data.weixin)
        wx.requestPayment({
          'timeStamp': res.data.data.weixin.timeStamp,
          'nonceStr': res.data.data.weixin.nonceStr,
          'package': res.data.data.weixin.package,
          'signType': 'MD5',
          'paySign': res.data.data.weixin.paySign,
          'success': function (res) {

          },
          'fail': function (res) {

          }
        })
      }
    })
  },
  focus_click: function (e) {
    this.setData({
      _num: e.target.dataset.num
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this;
    wx.request({
      url: app.data.Url +'goods/detail',
      data:{
        goodsid:options.goodsid
      },
      success:function(res){
        that.setData({
          pic: res.data.data.pic,
          
        })
        console.log(res)
      if(options.type==1){
        if (!res.data.data.specification[0]) {
          that.setData({
            detail: res.data.data,
            num: options.num,
            money: res.data.data.min_price * options.num,
            goodsid: options.goodsid,
          })
        }else{
          that.setData({
            detail: res.data.data,
            num: options.num,
            attrid: res.data.data.specification[options.rule].attrid,
            money: res.data.data.specification[options.rule].price * options.num,
            goodsid: options.goodsid,
          })
        }              
      }else{
        that.setData({
          detail: res.data.data,
          num: options.num,
          attrid: res.data.data.specification[options.rule].attrid,
          money: res.data.data.specification[options.rule].min_price * options.num,
          goodsid: options.goodsid,
          type:1
        })
        
      }
     }
    })
    wx.request({
      url: app.data.Url +'address/get_defaultadd',
      data:{
        uid:wx.getStorageSync('userInfo').uid,
      },
      method:'get',
      success:function(res){
        that.setData({
          add:res.data.data
        })
        console.log(res.data.data)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})